<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="上级菜单" prop="parentName">
        <el-popover
          ref="menuListPopover"
          placement="bottom-start"
          trigger="click">
          <el-tree
            :data="menuList"
            :props="menuListTreeProps"
            node-key="id"
            ref="menuListTree"
            @current-change="menuListTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false">
          </el-tree>
        </el-popover>
        <el-input v-model="dataForm.parentName" v-popover:menuListPopover :readonly="true"
                  placeholder="点击选择上级菜单"
                  class="menu-list__input"></el-input>
      </el-form-item>
      <el-form-item label="菜单名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="菜单名称"></el-input>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-radio-group v-model="dataForm.type" placeholder="请选择类型">
          <el-radio :label="0">默认</el-radio>
          <el-radio :label="1">外链接</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="菜单URL" prop="url" v-if="dataForm.type===sysConstant.OUT_MH_MENU_TYPE">
        <el-input v-model="dataForm.url" placeholder="菜单URL"></el-input>
      </el-form-item>
      <!--      <el-form-item label="菜单图标" prop="icon">-->
      <!--        <el-input v-model="dataForm.icon" placeholder="菜单图标"></el-input>-->
      <!--      </el-form-item>-->
      <el-form-item label="菜单类型" prop="showStatus">
        <el-radio-group v-model="dataForm.showStatus">
          <el-radio :label="0">标题菜单</el-radio>
          <el-radio :label="1">普通菜单</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="排序" prop="orderNum">
        <el-input v-model="dataForm.orderNum" placeholder="排序"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {treeDataTranslate} from '@/utils'

export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        parentId: '',
        name: '',
        url: '',
        type: 0,
        icon: '',
        orderNum: 0,
        parentName: '',
        showStatus:0
      },
      dataRule: {
        parentName: [
          {required: true, message: '上级菜单不能为空', trigger: 'change'}
        ],
        name: [
          {required: true, message: '菜单名称不能为空', trigger: 'blur'}
        ],
        url: [
          {required: true, message: '菜单URL不能为空', trigger: 'blur'}
        ],
        type: [
          {required: true, message: '类型不能为空', trigger: 'blur'}
        ],
        icon: [
          {required: true, message: '菜单图标不能为空', trigger: 'blur'}
        ],
        orderNum: [
          {required: true, message: '排序不能为空', trigger: 'blur'}
        ],
        showStatus: [
          {required: true, message: '不能为空', trigger: 'blur'}
        ],
      },
      sysConstant: this.sysConstant,
      menuList: [],
      menuListTreeProps: {
        label: 'name',
        children: 'children'
      }
    }
  },
  methods: {
    init(id) {
      //初始化数据
      this.getParentMenuList();

      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (!this.dataForm.id) {
          // 新增
          this.menuListTreeSetCurrentNode()
        } else {
          this.$http({
            url: this.$http.adornUrl(`/mh/menu/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.parentId = data.data.parentId
              this.dataForm.name = data.data.name
              this.dataForm.url = data.data.url
              this.dataForm.type = data.data.type
              this.dataForm.icon = data.data.icon
              this.dataForm.orderNum = data.data.orderNum
              this.dataForm.showStatus = data.data.showStatus
              this.menuListTreeSetCurrentNode()
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/mh/menu/${!this.dataForm.id ? 'save' : 'update'}`),
            method: !this.dataForm.id ? 'post' : 'put',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'parentId': this.dataForm.parentId,
              'name': this.dataForm.name,
              'url': this.dataForm.url,
              'type': this.dataForm.type,
              'icon': this.dataForm.icon,
              'orderNum': this.dataForm.orderNum,
              'showStatus':this.dataForm.showStatus
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    },

    //查询父级菜单
    getParentMenuList() {
      this.$http({
        url: this.$http.adornUrl(`/mh/menu/list`),
        method: 'get',
        params: this.$http.adornParams({'status': 0})
      }).then(({data}) => {
        if (data.success) {
          let dataList = data.data
          for (let i = 0; i < dataList.length; i++) {
            if (dataList[i].parentId && dataList[i].parentId > 0) {
              for (let j = 0; j < dataList.length; j++) {
                if (dataList[j].id === dataList[i].parentId) {
                  dataList[i].parentName = dataList[j].name;
                }
              }
            }
          }
          let menu = {};
          menu.id = 0;
          menu.name = "一级菜单";
          menu.parentId = 0;
          menu.open =  true;
          dataList.push(menu);
          this.menuList = treeDataTranslate(dataList, 'id')
        }
      })
    },

    // 菜单树选中
    menuListTreeCurrentChangeHandle(data, node) {
      this.dataForm.parentId = data.id
      this.dataForm.parentName = data.name
    },
    // 菜单树设置当前选中节点
    menuListTreeSetCurrentNode() {
      console.log(this.dataForm.parentId)
      this.$refs.menuListTree.setCurrentKey(this.dataForm.parentId)
      this.dataForm.parentName = (this.$refs.menuListTree.getCurrentNode() || {})['name']
      console.log(this.$refs.menuListTree)
    },

  }
}
</script>
